<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.hp.hpl.jena.query.*" %> 
<%@page import="com.hp.hpl.jena.rdf.model.*" %>
<%@page import="com.cseg674.datamanagement.adler.*" %>
<%@page import="com.hp.hpl.jena.util.iterator.ExtendedIterator" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<%@ include file="/includes/head.jsp" %>

<body onload="init()">

<div id="page">

	<%@ include file="/includes/header.jsp" %>
	
	<div id="menulinks">
		<a href="index.jsp"><span>Home</span></a>
		<div class="menulines"></div>
		<a href="about.jsp"><span>About</span></a>
		<div class="menulines"></div>
		<a href="entry.jsp"><span>Entry</span></a>
		<div class="menulines"></div>
		<a class="active" href="query.jsp"><span>Query</span></a>
		<div class="menulines"></div>
		<% if(null == session.getAttribute("username")) { %>
			<a href="login.jsp"><span>Login</span></a>
		<% } else { %>  
			<a href="logout.jsp"><span>Logout</span></a>
		<% } %>
		<div class="menulines"></div>
		<a href="engineers.jsp"><span>Engineers</span></a>
		<div class="menulines"></div>
	</div>
	
	<div id="mainarea">
		<div id="contentarea">
			<h2><img src="images/search_sm.png" alt="SPARQL Search" /> &nbsp; SPARQL Search</h2>
			<div id="query" class="content">

				<% ArrayList owlSubjList = (ArrayList)session.getAttribute("OWLSubjList"); %>
				<% ArrayList owlPredList = (ArrayList)session.getAttribute("OWLPredList"); %> 
				<% ArrayList owlObjList = (ArrayList)session.getAttribute("OWLObjList"); %>
				<% String prjName = (String)request.getAttribute("PrjName"); %>
				<% 
					String SubjOwl = request.getParameter("SubjOwl");
					if(SubjOwl == null) SubjOwl = "";
					String PredOwl = request.getParameter("PredOwl");
					if(PredOwl == null) PredOwl = "";
					String ObjOwl = request.getParameter("ObjOwl");
					if(ObjOwl == null) ObjOwl = "";
				%>
				
				<script type="text/javascript">
				function clearSelect(id) {
				    var subjContents = "<select id='SubjOwl' name='SubjOwl' size=10 onClick='handleOnClick(this, \"SubjOwl\");handleRdfData(this, \"SubjOwl\", \"<%=prjName %>\");setSubject(this);setSubjSelected();checkSubj();resetLiterals()'><% for(int i=0; i<owlSubjList.size(); i++) { %><option value='<%= owlSubjList.get(i) %>'><%= owlSubjList.get(i) %></option> <% } %> </select>";
					var predContents = "<select id='PredOwl' name='PredOwl' size=10 onClick='handleOnClick(this, \"PredOwl\");setProject(\"<%=prjName %>\");setPredicate(this);setPredSelected();checkPred();resetLiterals()'><% for(int j=0; j<owlPredList.size(); j++) { %><option value='<%= owlPredList.get(j) %>'><%= owlPredList.get(j) %></option><% } %></select>";
					var objContents = "<select id='ObjOwl' name='ObjOwl' size=10 onClick='handleOnClick(this, \"ObjOwl\");handleRdfData(this, \"ObjOwl\", \"<%=prjName %>\");setObject(this);setObjSelected();checkObj();resetLiterals()'><% for(int k=0; k<owlObjList.size(); k++) { %><option value='<%= owlObjList.get(k) %>'><%= owlObjList.get(k) %></option><% } %> </select>";
									
					document.getElementById("Subj").innerHTML = subjContents;
					document.getElementById("Pred").innerHTML = predContents;
					document.getElementById("Obj").innerHTML = objContents;
					document.TRIPLEForm.SubjList.disabled = true;
					document.TRIPLEForm.ObjList.disabled = true;
					subjSelected = false;
					predSelected = false;
					objSelected = false;
					
					if (id === "reset") { init(); }
				}
				</script>	
				
				<div>&nbsp;<i>Current location:</i>&nbsp;&nbsp;<a href="query.jsp">Project Select</a>&nbsp;&nbsp;>&nbsp;&nbsp;<b>SPARQL Search</b></div>
				<br />
				<form id="tripleForm" name="TRIPLEForm" >
					<table cellspacing="2" cellpadding="2"> 
						<tbody>
						<tr>
							<td align="center"><strong><font size="4">Subject</font></strong></td>
							<td align="center"><strong><font size="4">Predicate</font></strong></td>
							<td align="center"><strong><font size="4">Object</font></strong></td>
						</tr>
						<tr>
							<td align="center">
								<div id="Subj">
									<select id="SubjOwl" name="SubjOwl" size=10 onClick="handleOnClick(this, 'SubjOwl');handleRdfData(this, 'SubjOwl', '<%=prjName %>');setSubject(this);setSubjSelected();checkSubj()">
									<% for(int i=0; i<owlSubjList.size(); i++) { %>
									<option value='<%= owlSubjList.get(i) %>'><%= owlSubjList.get(i) %></option>
									<% } %>   
									</select>
								</div>
							</td>
							<td align="center">
								<div id="Pred">
									<select id="PredOwl" name="PredOwl" size=10 onClick="handleOnClick(this, 'PredOwl');setProject('<%=prjName %>');setPredicate(this);setPredSelected();checkPred()">
									<% for(int j=0; j<owlPredList.size(); j++) { %>
									<option value='<%= owlPredList.get(j) %>'><%= owlPredList.get(j) %></option>
									<% } %>  
					  				</select>
					  			</div>
					        </td>
							<td align="center">
								<div id="Obj">
									<select id="ObjOwl" name="ObjOwl" size=10 onClick="handleOnClick(this, 'ObjOwl');handleRdfData(this, 'ObjOwl', '<%=prjName %>');setObject(this);setObjSelected();checkObj()">
									<% for(int k=0; k<owlObjList.size(); k++) { %>
									<option value='<%= owlObjList.get(k) %>'><%= owlObjList.get(k) %></option>
									<% } %> 
									</select>
								</div>
							</td>
						</tr>
						<tr>
							<td align="center">
								<div id="SubjMenu">
									<select disabled id="SubjList" name="SubjList" style="margin: 10px;">
									<option>&nbsp;</option>
									</select>
								</div>
							</td>
							<td></td>
							<td align="center">
								<div id="ObjMenu">
									<select disabled id="ObjList" name="ObjList" style="margin: 10px;">
									<option>&nbsp;</option>
									</select>
								</div>
							</td>
						</tr>
						<tr>
							<td colspan="3">
								<p align="center">
									<input id="addRule" type="button" value="Form Query" name="addRule" onClick="setTable();checkLiteralSelect()">
									<input id="Reset" type="reset" value="Reset" name="Reset" onClick="clearSelect('reset')">&nbsp;
								</p>
							</td>
						</tr>
						</tbody>
					</table>
				</form>
			
				<form id="sparqlForm" name="SPARQLForm" action="SparqlQueryServlet" method="POST">
					<strong><font size="4">SPARQL Query</font></strong>
					<p><textarea readonly id="QueryArea" name="QueryArea" rows="14" cols="70" style="color:#000000; font-family: Verdana; font-size: 12pt;"></textarea>&nbsp;</p>
					<table>
						<tbody>
						<tr>
							<td>
								Limit results:  
								<select disabled id="limit" name="limit" style="width: auto; margin: 0;" onChange="setLimit(this)">
									<option>None</option>
									<option>5</option>
									<option>10</option>
									<option>20</option>
									<option>50</option>
									<option>100</option>
								</select>
								&nbsp;&nbsp;
							</td>
							<td>
								<div id="sortList">
									Sort by:
									<select disabled id="orderBy" name="orderBy" style="width: auto; margin: 0;" onChange="setOrderTarget(this)">
									<option>None</option>
									</select>
								</div>
							</td>
							<td>
								<select disabled id="sortMethod" name="sortMethod" style="width: auto; margin: 0;" onChange="setSortMethod(this)">
									<option>Ascending</option>
									<option>Descending</option>
								</select>
							</td>
						</tr>
						</tbody>
					</table>
					<p>
						<strong><font size="4">
						<input id="Search" type="submit" value="Submit Query" name="Search" onClick="return checkSubmit()">
						<input id="Clear" type="reset" value="Clear" name="Clear" onClick="clearQuery()">
						<input type="hidden" name="PrjName" value="<%=prjName %>">
						<input type="hidden" name="UserQuery" value="">
						<input type="hidden" name="ResultTitle" value="">
						<input type="hidden" name="TableSize" value="">
						<input type="hidden" name="NumSubmit" value="">
						</font></strong>
					</p>
				</form>

			</div>

		</div>
	
		<%@ include file="/includes/sidebar.jsp" %>
		<%@ include file="/includes/footer.jsp" %>
	</div>
	<script type="text/javascript" language="javascript" src="js/ajax.js"></script>
	<script type="text/javascript" language="javascript" src="js/sparql.js"></script>
</body>
</html>